package com.android.svmc.app.mobile.activity;

import luyenlv.pro.screen.base.BaseActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.android.svmc.app.mobile.adapter.MenuStoryAdapter;
import com.android.svmc.app.mobile.controller.CheckNewRecordService;
import com.android.svmc.app.mobile.controller.SaveDataService;
import com.android.svmc.app.mobile.tabmanager.TabGroupActivity;
import com.android.svmc.app.mobile.util.CommonDefine;
import com.android.svmc.app.mobile.util.SettingUtils;
import com.android.svmc.app.mobile.view.CustomDialog;
import com.android.svmc.app.mobile.view.FontTextView;

public class StoryMainMenuActivity extends BaseActivity implements CommonDefine {
	GridView menuStory;
	TabGroupActivity parentActivity;

	// ArrayList<MenuItem> listcategory= new ArrayList<MenuItem>();
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(getLayout(R.array.main_layout));
		initUI();
		parentActivity = (TabGroupActivity) getParent();
		Log.i("anhson", "onCreate() StoryMainMenuActivity");
		saveNameUser();

	}

	private void setOrientation() {
		if (SettingUtils.getbooleanPreferences(this, IS_ROTATION_SCREEN) == false) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
	}

	private void saveNameUser() {
		SharedPreferences mPreferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		boolean firstTime = mPreferences.getBoolean("firstTime_app", true);
		if (firstTime) {
			SharedPreferences.Editor editor = mPreferences.edit();
			editor.putBoolean("firstTime_app", false);
			editor.commit();
			CustomDialog submitNameComment = new CustomDialog(
					getDialogContext());
			submitNameComment.setDialogType(DIALOG_SUBMIT_NAME_COMMENT, "");
			submitNameComment.show();
			/*************************************************/

			Intent intent = new Intent(this, CheckNewRecordService.class);
			if (SettingUtils.getbooleanPreferences(this,
					IS_STARTED_SERVICE_NOTIFICATION) == false
					&& SettingUtils
							.getbooleanPreferences(this, IS_NOTIFICATION) == true) {
				startService(intent);
			}

		}

	}

	private void initUI() {
		TextView titlebar = (TextView) findViewById(R.id.titlebar_txt_title);
		FontTextView.setfontHelveticaBold(titlebar, this);
		titlebar.setText(getString(R.string.tab_story));
		menuStory = (GridView) findViewById(R.id.main_gridview_listmenu);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MenuStoryAdapter mydapter = new MenuStoryAdapter(this,
				Splash.listcategory);
		menuStory.setAdapter(mydapter);
		listenClickListView(menuStory);
		setOrientation();
		Log.i("anhson", "onResume() StoryMainMenuActivity");
		if (SettingUtils.getbooleanPreferences(this, IS_OFFLINE_MODE) == true
				&& SettingUtils.getbooleanPreferences(this,
						IS_STARTED_SERVICE_OFFLINE_MODE) == false) {
			saveDataOffline();
		}
	}

	private void saveDataOffline() {
		Intent intent = new Intent(this, SaveDataService.class);
		startService(intent);
	}

	public void listenClickListView(GridView ls) {
		ls.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int postion, long id) {
				Intent i = new Intent(StoryMainMenuActivity.this,
						ListPostInCategory.class);
				i.putExtra(ID_CATEGORY, Splash.listcategory.get(postion)
						.getId());
				i.putExtra(TITLE, Splash.listcategory.get(postion).getTitle());
				parentActivity.startChildActivity("ListPostInCategory", i);
			}
		});
	}

	@Override
	public void clearMemory() {
		unbindReferences(this, R.id.main_rl);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("anhson", "onDestroy() StoryMainMenuActivity");
	}

	/**
	 * Get context in groupActivity.Use for show dialog
	 * 
	 * @return : context
	 */
	private Context getDialogContext() {
		Context mcontext;
		if (getParent() != null)
			mcontext = getParent();
		else
			mcontext = this;
		return mcontext;
	}

}